module WindSpirit
{
    export class RankLayer extends WindSpirit.MarskLayer
    {
        public title: egret.TextField;
        public line1: egret.Shape;
        public desc: egret.TextField;
        public btnShare: WindSpirit.IButton;
        public line2: egret.Shape;
        public rankLayer: egret.DisplayObjectContainer;
        public loading: WindSpirit.LoadingSprite;
        public shareNumber: number = 0;
        public rank: number = 0;
        public constructor()
        {
            super();
            var title = new egret.TextField;
            title.size = 50;
            title.text = "排行榜";
            title.anchorX = 0.5;

            title.x = this.container.width >> 1;
            title.textColor = 0xffffff;
            this.title = title;
            title.y = 30;
            this.container.addChild(title);

            var line1 = new egret.Shape;
            line1.graphics.beginFill(0xffffff);
            line1.graphics.drawRect(0,0,400,5);
            line1.anchorX = 0.5;
            line1.y = title.height + title.y + 20;
            line1.x = this.container.width >> 1;
            this.line1 = line1;
            this.container.addChild(line1);

            var desc = new egret.TextField;
            desc.size = 25;
            desc.textColor = 0xffffff;
            desc.textAlign = egret.HorizontalAlign.CENTER;
            desc.fontFamily = WindSpirit.Const.FONT_FAMILY;
            desc.lineSpacing = 15;
            desc.anchorX = 0.5;
            desc.x = this.container.width >> 1;
            desc.width = 350;
            this.desc = desc;
            this.updateDesc();
            desc.y = line1.height + line1.y + 10;
            this.container.addChild(desc);

            var btnShare = new WindSpirit.IButton("分享");
            btnShare.addEventListener(egret.TouchEvent.TOUCH_TAP,this.share,this);
            btnShare.y = desc.height + desc.y + 10;
            btnShare.anchorX = 0.5;
            btnShare.x = this.container.width >> 1;
            this.btnShare = btnShare;
            this.container.addChild(btnShare);

            var line2 = new egret.Shape;
            line2.graphics.beginFill(0xffffff);
            line2.graphics.drawRect(0,0,400,5);
            line2.anchorX = 0.5;
            line2.x = this.container.width >> 1;
            line2.y = btnShare.height + btnShare.y + 10;
            this.line2 = line2;
            this.container.addChild(line2);

            var rankLayer = new egret.DisplayObjectContainer;
            rankLayer.y = line2.height + line2.y + 10;
            this.rankLayer = rankLayer;
            this.container.addChild(rankLayer);

            var loading = new WindSpirit.LoadingSprite;
            this.loading = loading;
            this.addChild(loading);
            WindSpirit.Net.getRankList(this.rankComplete,this);
            WindSpirit.Net.getMyRank(this.onMyRank,this);
            WindSpirit.Net.getShareNumber(this.onShareNumber,this);
            this.touchEnabled = true;
            this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this);
        }
        public touch()
        {
            PopUpManager.removePopUp(this,9,300);
        }
        public share(e:egret.TouchEvent)
        {
            e.stopPropagation();
            this.addChild(new WindSpirit.ShareTipLayer);
        }
        public onMyRank(data)
        {
            console.log(data.target.data);
            data = JSON.parse(data.target.data);
            this.rank = data.rank;
            this.updateDesc();
        }
        public onShareNumber(data)
        {
            console.log(data.target.data);
            data = JSON.parse(data.target.data);
            this.shareNumber = data.total;
            this.updateDesc();
        }
        public updateDesc()
        {
            this.desc.text = "有" + this.shareNumber + "位英雄在你的号召下\n参加了武林大会！\n你在众英雄中排第" + this.rank + "名！";
        }
        
        public rankComplete(a)
        {
            this.removeChild(this.loading);
            console.log(a.target.data);
            var ret = JSON.parse(a.target.data);
            var list = ret.list;
            var len = list.length;
            var content = new egret.DisplayObjectContainer();
            for(var i = 0;i < len;i++)
            {
                var item = new WindSpirit.RankItem(list[i].nickname,list[i].score,i + 1);
                item.anchorX = 0.5;
                item.x = this.container.width >> 1;
                item.y = item.height * i;
                content.addChild(item);
            }
            var scroll = new egret.ScrollView();
            scroll.setContent(content);
            this.rankLayer.addChild(scroll);
        }
    }
}